<?php

namespace app\index\controller\v1\wechat;


use app\common\controller\WechatController;
use app\common\model\diy\AddonDiy;

class Diy extends WechatController
{

    public function getDiy()
    {
        $Diy = new \app\common\model\diy\Diy();
        $diy_data = $Diy->where(['key' => input('key'), 'type' => input('type', 'wechat')])->find();
        if (!$diy_data) {
            return $this->error('error');
        }
        $data = $diy_data->value;
        $rule = $data['rule'];
        $dk_rule = [];
        foreach ($rule as $key => $value) {
            if (isset($value['setting'])) {
                $dk_rule[$key] = array(
                    'display'  => true,
                    'hidden'   => false,
                    'props'    => ['setting' => $value['setting']],
                    'type'     => "dkRow",
                    'children' => [$value]
                );
            } else {
                $dk_rule[$key] = array(
                    'display'  => true,
                    'hidden'   => false,
                    'props'    => ['setting' => ['bg_color' => '']],
                    'type'     => "dkRow",
                    'children' => [$value]
                );
            }

        }
        $data['rule'] = $dk_rule;

        return $this->success('ok', $data);
    }

    public function getAddonDiy()
    {
        $Diy = new AddonDiy();
        $plaid = get_plaid();
        if (!$plaid) {
            return $this->error("平台不存在");
        }
        $diy_data = $Diy->where(['key' => input('key'), 'type' => input('type', 'wechat'), 'plaid' => $plaid,'addon'=>get_addon()])->find();
        if (!$diy_data) {
            return $this->error('error');
        }
        $data = $diy_data->value;
//        $rule = $data['rule'];
//        $dk_rule = [];
//        foreach ($rule as $key => $value) {
//            if (isset($value['setting'])) {
//                $dk_rule[$key] = array(
//                    'display'  => true,
//                    'hidden'   => false,
//                    'props'    => ['setting' => $value['setting']],
//                    'type'     => "dkRow",
//                    'children' => [$value]
//                );
//            } else {
//                $dk_rule[$key] = array(
//                    'display'  => true,
//                    'hidden'   => false,
//                    'props'    => ['setting' => ['bg_color' => '']],
//                    'type'     => "dkRow",
//                    'children' => [$value]
//                );
//            }
//
//        }
//        $data['rule'] = $dk_rule;
        return $this->success('ok', dealWithDiyData($data),$data);
    }

}
